package com.xmb.wechat.view.wechat.setting;

import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatExitApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.entity.WechatAppInfoBean;
import com.xmb.wechat.common.Constant;
import com.xmb.wechat.definterface.WhoSendListener;
import com.xmb.wechat.dialog.ChangeFontSizeHintDialog;
import com.xmb.wechat.picasso.PicassoRoundByPercentTransform;
import com.xmb.wechat.widget.FontSizeView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/7 10:34
 */
public class WeChatChangeFontSizeActivity extends BaseActivity {
    //修改字体Activity中的文字大小
    private final int[] mTextSize = new int[]{
            13,
            14,
            15,
            17,
            19,
            21,
            24,
            27,
            30
    };

    @BindView(R2.id.iv_avatar1)
    ImageView    mIvAvatar1;
    @BindView(R2.id.tv_1)
    TextView     mTv1;
    @BindView(R2.id.iv_avatar2)
    ImageView    mIvAvatar2;
    @BindView(R2.id.tv_2)
    TextView     mTv2;
    @BindView(R2.id.iv_avatar3)
    ImageView    mIvAvatar3;
    @BindView(R2.id.tv_3)
    TextView     mTv3;
    @BindView(R2.id.font_size_view)
    FontSizeView mFontSizeView;

    private ChangeFontSizeHintDialog mDialog;
    private int                      mFontScaleLevel;
    private int                      mLastFontScaleLevel;//上次设置的文字大小

    public WeChatChangeFontSizeActivity() {
        super(R.layout.wechat_activity_change_font_size);
    }

    @Override
    protected void initView() {
        //滑动返回监听
        mFontSizeView.setChangeCallbackListener(this::adjustViewSize);
        //设置当前文字大小
        mLastFontScaleLevel = SPUtils.getInstance().getInt(Constant.SP.SP_KEY_FONT_SCALE_LEVEL, Constant.SP.SP_DEFAULT_FONT_SCALE_LEVEL);
        mFontSizeView.setDefaultPosition(mLastFontScaleLevel);
        adjustViewSize(mLastFontScaleLevel);
    }

    @Override
    protected void onResume() {
        super.onResume();
        WechatAppInfoBean vo = WechatAppInfoBean.getWechat(this);
        Picasso.get().load("file:" + vo.getAvatar()).error(R.drawable.ic_wx_default_avatar).transform(new PicassoRoundByPercentTransform(12)).into(mIvAvatar1);
    }

    private void adjustViewSize(int position) {
        mFontScaleLevel = position;
        if (mFontScaleLevel < Constant.mChatIconWidth.length) {
            //textView大小
            mTv1.setTextSize(mTextSize[mFontScaleLevel]);
            mTv2.setTextSize(mTextSize[mFontScaleLevel]);
            mTv3.setTextSize(mTextSize[mFontScaleLevel]);
            //View大小
            int result = Float.valueOf(Constant.mChatIconWidth[mFontScaleLevel] * ScreenUtils.getScreenWidth()).intValue();
            ViewGroup.LayoutParams layoutParams1 = mIvAvatar1.getLayoutParams();
            layoutParams1.width= result;
            layoutParams1.height= result;
            mIvAvatar1.setLayoutParams(layoutParams1);

            ViewGroup.LayoutParams layoutParams2 = mIvAvatar2.getLayoutParams();
            layoutParams2.width= result;
            layoutParams2.height= result;
            mIvAvatar2.setLayoutParams(layoutParams2);

            ViewGroup.LayoutParams layoutParams3 = mIvAvatar3.getLayoutParams();
            layoutParams3.width= result;
            layoutParams3.height= result;
            mIvAvatar3.setLayoutParams(layoutParams3);
        }
    }

    @OnClick(R2.id.iv_back)
    public void onViewClicked() {
        doExit();
    }

    @Override
    public void onBackPressed() {
        doExit();
    }

    private void doExit() {
        if (mFontScaleLevel != mLastFontScaleLevel) {//有修改
            if (mDialog == null)
                mDialog = new ChangeFontSizeHintDialog(this, new WhoSendListener() {
                    @Override
                    public void onMe() {
                        SPUtils.getInstance().put(Constant.SP.SP_KEY_FONT_SCALE_LEVEL, mFontScaleLevel, true);
                        WechatExitApplication.getInstance().exit();
                    }

                    @Override
                    public void onOther() {
                        WeChatChangeFontSizeActivity.this.finish();
                    }
                });
            mDialog.show();
        } else {
            WeChatChangeFontSizeActivity.this.finish();
        }
    }
}
